{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This tutorial describes the basics of assigning users to projects, workspaces, and specific tasks in Label Studio Enterprise using the Python SDK."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Connect to Label Studio Enterprise"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Start by connecting to Label Studio Enterprise using the `LabelStudio` client:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from label_studio_sdk.client import LabelStudio\n",
    "\n",
    "ls = LabelStudio(base_url=os.getenv('LABEL_STUDIO_URL', 'http://localhost:8080'), api_key=os.getenv('LABEL_STUDIO_API_KEY'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Get all users"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "users = list(ls.users.list())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Get project by ID"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "PROJECT_ID = int(os.getenv('LABEL_STUDIO_PROJECT_ID', '248'))\n",
    "project = ls.projects.get(id=PROJECT_ID)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Get project members"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "members = list(ls.organizations.members.list(id=int(os.getenv('LABEL_STUDIO_ORG_ID', '1'))))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Add user as a project member"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "user = users[0]\n",
    "res = ls.projects.members.bulk.post(id=PROJECT_ID, all_=False, included=[user.id], roles=[{'role': 'AN', 'user_id': user.id}])  # AN = annotator\n",
    "res"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Change user role"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can change the role of a user in the currently active organization."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ORG_ID = int(os.getenv('LABEL_STUDIO_ORG_ID', '1'))\n",
    "res = ls.organizations.members.update(ORG_ID, user_id=user.id, role='RE')  # RE = reviewer\n",
    "res"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Assign tasks to annotators or reviewers"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can assign tasks to annotators so that they can work on specific tasks. You must be using manual task distribution in your project to assign tasks."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from label_studio_sdk.projects.assignments import (\n",
    "    AssignmentsBulkAssignRequestSelectedItemsIncluded,\n",
    ")\n",
    "\n",
    "tasks = ls.tasks.list(project=PROJECT_ID, fields='id')\n",
    "tasks_ids = [t.id for t in tasks]\n",
    "# choose 'AN' or 'RE'\n",
    "ls.projects.assignments.bulk_assign(id=PROJECT_ID, type='AN', users=[u.id for u in users], selected_items=AssignmentsBulkAssignRequestSelectedItemsIncluded(all_=False, included=tasks_ids))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can assign tasks to reviewers so that they can review the annotations for specific tasks."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ls.projects.assignments.create(project=PROJECT_ID, type='RE', users=[u.id for u in users], selected_items={'all': False, 'included': tasks_ids})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## List all tasks assigned to specific annotators or reviewers"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Before you assign new tasks to annotators, you might want to know which or how many tasks an annotator already has to work on. To list all the tasks already assigned to an annotator, run the following code:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from label_studio_sdk.data_manager import Filters, Column, Operator, Type\n",
    "\n",
    "filters = Filters.create(Filters.OR, [\n",
    "    Filters.item(\n",
    "        Column.annotators,  # or Column.reviewers\n",
    "        Operator.CONTAINS,\n",
    "        Type.List,\n",
    "        Filters.value(user.id)\n",
    "    )\n",
    "])\n",
    "tasks = list(ls.tasks.list(project=PROJECT_ID, query=filters, fields='id'))\n",
    "print([t.id for t in tasks])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Remove annotators or reviewers from tasks"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To remove annotators from a task, for example to change who is assigned to a specific task, or remove an annotator that no longer works on the project, run the following code:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tasks = ls.tasks.list(project=PROJECT_ID, fields='id')\n",
    "tasks_ids = [t.id for t in tasks]\n",
    "for task_id in tasks_ids:\n",
    "    ls.projects.assignments.delete(id=PROJECT_ID, type='AN', task_pk=task_id)  # 'AN' or 'RE'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
